from apps.goods.models import SKU, SKUSpecification
from rest_framework import serializers


class SKUSpecificationModelSerializer(serializers.ModelSerializer):
    spec_id = serializers.IntegerField()
    option_id = serializers.IntegerField()

    class Meta:
        model = SKUSpecification
        fields = ['spec_id', 'option_id']


from apps.goods.models import SKU, GoodsCategory
from rest_framework import serializers

class SKUModelSerializer(serializers.ModelSerializer):

    # category_id 对应的外键值
    category_id = serializers.IntegerField()
    # category 对应模型的 __str__ 返回的数据
    category = serializers.StringRelatedField()

    spu_id = serializers.IntegerField()
    spu = serializers.StringRelatedField()

    specs = SKUSpecificationModelSerializer(many=True)

    class Meta:
        model = SKU
        # fields = ['id','name',...]
        fields = '__all__'

    def create(self, validated_data):

        # 1.把“1对多”的“多”的数据popc出来，单独用一个变量接收
        specs = validated_data.pop('specs')

        from django.db import transaction
        # with transaction.atomic(): 事务 可以自动进行事务的提交和回滚，不用异常捕获，异常自动回滚
        with transaction.atomic():
            save_point = transaction.savepoint()
            try:

                # 2.validated_data 就剩下sku的数据
                sku = SKU.objects.create(**validated_data)
                # 3.对“多”的数据进行遍历保存
                for spec in specs:
                    # spec = {spec_id:'4',option_id:'8'}
                    SKUSpecification.objects.create(sku=sku,**spec)

            except Exception:
                transaction.savepoint_rollback(save_point)
            else:
                transaction.savepoint_commit(save_point)

        return sku


    def update(self, instance, validated_data):
        # 1.把“1对多”的“多”的数据 pop出来
        specs = validated_data.pop('specs')
        # 2.validated_data 数据就只剩下 sku的数据
        super().update(instance,validated_data)
        # 3.遍历“多”的数据，进行更新
        for spec in specs:
            # spec = {"spec_id": "规格id", "option_id": "选项id"}
            # instance 就是 具体的sku
            # 我们不支持修改 规格. 查询的时候也要查询对应的规格
            SKUSpecification.objects.filter(sku=instance,spec_id=spec.get('spec_id')).update(option_id=spec.get('option_id'))

        return instance



class GoodsCategoryModelSerializer(serializers.ModelSerializer):

    class Meta:
        model = GoodsCategory
        fields = ['id','name']


# SimpleSPU###############
from apps.goods.models import SPU
class SimpleSPUModelSerializer(serializers.ModelSerializer):

    class Meta:
        model = SPU
        fields = ['id','name']



from apps.goods.models import SpecificationOption
class SpecificationOptionModelSerializer(serializers.ModelSerializer):

    class Meta:
        model = SpecificationOption
        fields = ['id','value']


from apps.goods.models import SPUSpecification

class SPUSpecModelSerializer(serializers.ModelSerializer):

    spu = serializers.StringRelatedField()
    spu_id = serializers.IntegerField()

    options = SpecificationOptionModelSerializer(many=True)

    class Meta:
        model = SPUSpecification
        fields = ['id','name','spu','spu_id','options']






